<template>
  <div class="item-config">
        <el-button type="primary"
                  style="width:165px;"
                  size="small"
                  @click="startAuth">{{ $t('custom.connector.syncAuthorization') }}</el-button>
  </div>
</template>

<script>
import { Component } from './modules/Component.js'
import Trail from '@/components/storybook/mixins/trail'

export default {
  mixins: [Trail],

  props: {
    ameritradeConfigForm: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {}
  },
  methods: {
    startAuth () {
      const ameritradeURL = `https://auth.tdameritrade.com/auth?response_type=code&redirect_uri=${window.location.origin}/web/um/am/redirect&client_id=MIEDLMZ3D5JG59TF7XXW9KMPC6CG97BZ%40AMER.OAUTHAP&state=${this.$store.state.user.tenantCode}_${this.$store.state.user.id}`
      window.open(ameritradeURL, '_blank')
    },
    synctdData (itemInfo) {
      const params = {
        tenantId: this.$store.state.user.tenantCode,
        subjectCode: 'AMERITRADE'
      }
      this.$interface.taskManager.syncClientData(params).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (itemInfo && res.data && res.data.sync) {
          itemInfo.sync.lastTime = res.data.sync.lastTime
          itemInfo.sync.status = res.data.sync.status
        }
        Component.$emit('getConnectorConfig')
        return this.$message.success(res.msg)
      }).catch(() => {
      })
    }

  }
}
</script>

<style scoped></style>
